package com.sanpu.kxe.business.main.adapter;

import android.text.TextUtils;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.sanpu.kxe.R;
import com.sanpu.kxe.business.main.bean.PromotePosterOutput;
import com.sanpu.kxe.business.main.interfaces.IOnPromoteClickListener;
import com.sanpu.kxe.common.view.ExpandableTextView;
import com.sanpu.kxe.common.view.MaterialImageView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Description：推广海报
 * Created by 薛勇军 on 2018/8/20.
 */

public class PromotePosterAdapter extends BaseQuickAdapter<PromotePosterOutput, BaseViewHolder> {

    private IOnPromoteClickListener mPromoteClickListener;

    public PromotePosterAdapter(IOnPromoteClickListener mPromoteClickListener) {
        super(R.layout.item_promote_poster, new ArrayList<PromotePosterOutput>());
        this.mPromoteClickListener = mPromoteClickListener;
    }

    @Override
    protected void convert(BaseViewHolder helper, PromotePosterOutput item) {
        if (item != null) {
            final int currentPosition = helper.getLayoutPosition();
            final String mtlId = item.getMtlId();
            String tmsmp = item.getAddDt();
            if (!TextUtils.isEmpty(tmsmp)) {
                if (tmsmp.contains(" ")) {
                    tmsmp = tmsmp.substring(0, tmsmp.indexOf(" "));
                }
                String[] dateTime = tmsmp.split("-");
                if (dateTime.length == 3) {
                    helper.setText(R.id.materialDay, dateTime[2])
                            .setText(R.id.materialMonth, dateTime[1] + "月")
                            .setText(R.id.materialYear, dateTime[0] + "年");
                }
            }
            final String title = item.getBody();
            ExpandableTextView materialTitle = helper.getView(R.id.materialTitle);
            if (!TextUtils.isEmpty(title)) {
                materialTitle.setText(title, currentPosition);
            }

            helper.getView(R.id.materialCopy).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mPromoteClickListener != null) {
                        mPromoteClickListener.onCopy(title);
                    }
                }
            });
            MaterialImageView vMaterialImage = helper.getView(R.id.materialImage);
            vMaterialImage.setVisibility(View.GONE);
            if (item.getPictures() != null) {
                try {
                    String[] pictures = item.getPictures().split(",");
                    List<String> arraylist = Arrays.asList(pictures);
                    final ArrayList<String> arrPictures = new ArrayList<>(arraylist);
                    if (arrPictures.size() > 0) {
                        vMaterialImage.setVisibility(View.VISIBLE);
                        vMaterialImage.setLineCountStyle(3, arrPictures.size());
                        vMaterialImage.createMenu(arrPictures, mPromoteClickListener);
                    }
                    helper.getView(R.id.materialSave).setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if (mPromoteClickListener != null) {
                                mPromoteClickListener.onSave(arrPictures, title, mtlId, currentPosition);
                            }
                        }
                    });
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            helper.setText(R.id.materialNum, !TextUtils.isEmpty(item.getDowloadNum())
                    ? "分享数 " + item.getDowloadNum() : "分享数 0");
        }
    }

    /**
     * 刷新列表
     */
    public void setNotifyData(int position, String downloadNum) {
        List<PromotePosterOutput> outputs = getData();
        if (outputs.size() > position && !TextUtils.isEmpty(downloadNum)) {
            outputs.get(position).setDowloadNum(downloadNum);
        }
        notifyDataSetChanged();
    }
}
